//
//  CXLEmoticonAttachment.swift
//  Emotion
//
//  Created by 曹晓龙 on 15/10/24.
//  Copyright © 2015年 曹晓龙. All rights reserved.
//

import UIKit

class CXLEmoticonAttachment : NSTextAttachment {
    
    
    var emoticon:CXLEmoticon
    
    init(emoticon:CXLEmoticon){
        
        self.emoticon = emoticon
        
        super.init(data: nil, ofType: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func getImageText(font:UIFont) ->NSMutableAttributedString{
        
        image = UIImage(contentsOfFile: emoticon.pngPath)
        
        // 文字的线高
        let height = font.lineHeight
        // 设置图片的偏移
        bounds = CGRect(x: 0, y: -4, width: height, height: height)
        
        // 获取图片的属性文本
        let imageText = NSMutableAttributedString(attributedString: NSAttributedString(attachment: self))
        
        imageText.addAttribute(NSFontAttributeName, value: font, range: NSRange(location: 0, length: 1))
        
        return imageText
    }
}
